package com.simpletaskapp.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.simpletaskapp.R;
import com.simpletaskapp.constants.Constants;
import com.simpletaskapp.domain.Task;
import com.simpletaskapp.persistence.TaskDAO;

public abstract class AbstractTaskActivity extends Activity {

	private AdView adView;

	protected Task task;

	protected EditText descriptionEditText;

	protected TaskDAO taskDAO;

	protected abstract String getDisplayTitle();

	protected abstract int getMenuResource();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitle(getDisplayTitle());

		if (savedInstanceState != null)
			task = (Task) savedInstanceState.getSerializable("TASK");

		taskDAO = new TaskDAO(this);
		taskDAO.open();

		setContentView(R.layout.task_create_edit);
		descriptionEditText = (EditText) findViewById(R.id.task_description);

		// ADMOB
		adView = new AdView(this, AdSize.BANNER, Constants.ADMOB_EDITOR_ID);
		LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
		layout.addView(adView);
		adView.loadAd(new AdRequest());
	}

	protected String getDescriptionText() {
		return descriptionEditText.getText().toString();
	}

	private boolean getSaveVisbilityCondition() {
		String description = getDescriptionText();
		String oldDescription = task.getDescription();

		return !getDescriptionText().equals("")
				&& !description.equals(oldDescription);
	}

	@Override
	public void onBackPressed() {
		if (getSaveVisbilityCondition())
			saveOrUpdateTask();
		this.finish();
	}

	protected void saveOrUpdateTask() {
		String description = getDescriptionText();

		task.setDescription(description);
		taskDAO.saveOrUpdateTask(task);
		Toast.makeText(this, getString(R.string.saved), Toast.LENGTH_SHORT)
				.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(getMenuResource(), menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem saveItem = menu.findItem(R.id.saveTask);
		saveItem.setEnabled(getSaveVisbilityCondition());
		
		MenuItem cancelItem = menu.findItem(R.id.cancelTask);
		cancelItem.setVisible(getSaveVisbilityCondition());
		
		MenuItem backItem = menu.findItem(R.id.backTask);
		backItem.setVisible(!getSaveVisbilityCondition());
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.saveTask:
			saveOrUpdateTask();
			this.finish();
			break;

		case R.id.cancelTask:
			Toast.makeText(this, getString(R.string.canceled),
					Toast.LENGTH_SHORT).show();
			this.finish();
			break;
			
		case R.id.backTask:
			this.finish();
			break;
		}

		return true;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putSerializable("TASK", task);
	}

	@Override
	protected void onPause() {
		taskDAO.close();
		super.onPause();
	}

	@Override
	protected void onResume() {
		taskDAO.open();
		super.onResume();
	}

	@Override
	public void onDestroy() {
		adView.destroy();
		taskDAO.close();
		super.onDestroy();
	}
}
